package xkp.lesson4;

import java.io.*;

public class SerialDeepCopy {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Car car = new Car("123");
        Driver driver1 = new Driver(20,"张三",car);

/*        ObjectOutputStream outputStream = new ObjectOutputStream(
                new FileOutputStream("Lesson25/file/Driver"));*/
        //ByteArrayOutputStream 顾名思义：字节数组输出流
        // 该流对象在内存中维护一个字节数组，所以在该流中可以临时保存字节数据
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(buf);
        //把对象序列化保存在ByteArrayOutputStream对象的字节数组中
        outputStream.writeObject(driver1);

        /*ObjectInputStream inputStream = new ObjectInputStream(
                new FileInputStream("Lesson25/file/Driver"));*/
        //ByteArrayInputStream可以直接把内存中的数据作为数据源
        ByteArrayInputStream byteArrayinputStream = new ByteArrayInputStream(buf.toByteArray());
        ObjectInputStream inputStream = new ObjectInputStream(byteArrayinputStream);
        Driver driver2 = (Driver) inputStream.readObject();

        System.out.println(driver1);
        driver1.getCar().setCarNumber("234");
        System.out.println(driver2);

        inputStream.close();
        outputStream.flush();
        outputStream.close();
    }
}
